// 可视域分析
import { globalLayer } from '../BQGIS-globalVar'

let index = 0

export function initGraphicLayer(viewer = window.viewer) {
  if (!globalLayer.viewShedLayer) {
    globalLayer.viewShedLayer = new mars3d.layer.GraphicLayer()
    viewer.addLayer(globalLayer.viewShedLayer)
  }
  return globalLayer.viewShedLayer
}

// 获取可视域列表
export function getGraphics() {
  if (globalLayer.viewShedLayer) {
    return globalLayer.viewShedLayer.getGraphics()
  } else {
    return []
  }
}

export function flyTo(id, viewer = window.viewer) {
  const graphic = getGraphicById(id);
  const range = 1500;
  const bounding = new Cesium.BoundingSphere(graphic.position, range);
  viewer.camera.flyToBoundingSphere(bounding, {
    offset: new Cesium.HeadingPitchRange(
      viewer.camera.heading,
      viewer.camera.pitch,
      range
    ),
  });
  viewer.flyTo(graphic);
}

export function removeGraphic(id) {
  const graphic = getGraphicById(id)
  globalLayer.viewShedLayer.removeGraphic(graphic, true)
  if (viewer.scene.requestRenderMode) {
    viewer.scene.requestRender();
  }
}

// 添加可视域
export function startDrawGraphic(option) {
  initGraphicLayer()
  const name = option.name ? option.name : '可视域_' + ++index
  // 开始绘制
  return globalLayer.viewShedLayer.startDraw({
    type: "viewShed",
    name,
    style: {
      angle: 60,
      angle2: 45,
      distance: 80,
      heading: 44,
      pitch: -12,
      addHeight: 0.5 // 在坐标点增加的高度值，规避遮挡，效果更友好
    }
  })
}

// 属性编辑
export function getGraphicById(graphicId) {
  return globalLayer.viewShedLayer.getGraphicById(graphicId)
}

let selectedView
export function selGraphic(graphicId) {
  selectedView = globalLayer.viewShedLayer.getGraphicById(graphicId)
  return selectedView
}

export function selCamera(viewer = window.viewer) {
  if (!selectedView) {
    return
  }

  globalLayer.viewShedLayer.startDraw({
    type: "point",
    success: (graphic) => {
      const point = graphic.point
      graphic.remove() // 删除绘制的点

      selectedView.position = point
    }
  })
}

// 修改水平角度
export function onChangeAngle(value) {
  if (selectedView) {
    selectedView.angle = value
  }
  if (viewer.scene.requestRenderMode) {
    viewer.scene.requestRender();
  }
}

// 修改垂直角度
export function onChangeAngle2(value) {
  if (selectedView) {
    selectedView.angle2 = value
  }
  if (viewer.scene.requestRenderMode) {
    viewer.scene.requestRender();
  }
}

// 修改投射距离
export function onChangeDistance(value) {
  if (selectedView) {
    selectedView.distance = value
  }
  if (viewer.scene.requestRenderMode) {
    viewer.scene.requestRender();
  }
}

// 修改四周距离 value 修改后的数值
export function onChangeHeading(value) {
  if (selectedView) {
    selectedView.heading = value
  }
  if (viewer.scene.requestRenderMode) {
    viewer.scene.requestRender();
  }
}

//  修改俯仰角数值   value 修改后的数值
export function onChangePitch(value) {
  if (selectedView) {
    selectedView.pitch = value
  }
  if (viewer.scene.requestRenderMode) {
    viewer.scene.requestRender();
  }
}

//   线框是否显示   isCheckde 修改后的数值
export function showFrustum(isCheckde) {
  if (selectedView) {
    selectedView.showFrustum = isCheckde
  }
  if (viewer.scene.requestRenderMode) {
    viewer.scene.requestRender();
  }
}


// 修改视频的透明度   opacity 透明度数值
export function onChangeOpacity(opacity) {
  if (selectedView) {
    selectedView.opacity = opacity
  }
  if (viewer.scene.requestRenderMode) {
    viewer.scene.requestRender();
  }
}
